'use strict';
import * as Koa from 'koa';
import { router, prefix } from '../middleware/router';
import { response } from '../middleware/result';
import { merchant_wineService } from '../service/merchant_wine';
import { merchantService } from '../service/merchant';
import { customer_orderService } from '../service/customer_order';
import { merchantapplywineService } from '../service/MerchantApplyWine';
import { ObjectId } from 'bson';
/** 餐厅酒水API */
@prefix('merchantwine')
class Merchant_wineController {
    /**
    * @api {post} /merchantwine/save  保存酒水
    * @apiDescription 保存酒水
    * @apiName save
    * @apiParam (参数) {String} doc  酒水对象tsc
    * @apiSampleRequest /merchantwine/save
    * @apiGroup merchantwine
    * @apiVersion 1.0.0
    */
    @router('/save', { method: 'post' })
    async save(ctx: Koa.Context) {

        let result = await merchantService.queryall();
        result.forEach((val, idx, array) => {
            ctx.request.body.merchantid = val._id;
            let data = merchant_wineService.save(ctx.request.body);
        });

        response(ctx, "data");

    }

    /**
     * @api {post} /merchantwine/querypage  查询商户酒水列表（小程序）
     * @apiDescription 查询商户酒水列表（小程序）
     * @apiName querypage
     * @apiParam (参数) {String} offset   页码
     * @apiParam (参数) {String} limit    每页显示条数
     * @apiParam (参数) {String} mid    商户id
     * @apiSampleRequest /merchantwine/querypage
     * @apiGroup merchantwine
     * @apiVersion 1.0.0
     */
    @router('/querypage', { method: 'post' })
    async querypage(ctx: Koa.Context) {
        let result = await merchant_wineService.querypage(ctx.request.body);
        response(ctx, result);
    }

    /**
     * @api {post} /merchantwine/querywinebymid  查询商户酒水
     * @apiDescription 查询商户酒水
     * @apiName querywinebymid
     * @apiParam (参数) {String} mid  商户id
     * @apiParam (参数) {String} token  用户token
     * @apiSampleRequest /merchantwine/querywinebymid
     * @apiGroup merchantwine
     * @apiVersion 1.0.0
     */
    @router('/querywinebymid', { method: 'post' })
    async querywinebymid(ctx: Koa.Context) {
        let result = await merchant_wineService.queryall(ctx.request.body);
        response(ctx, result);
    }


    @router('/queryonebymid', { method: 'post' })
    async queryonebymid(ctx: Koa.Context) {
        let result = await merchant_wineService.querybymerchantid(ctx.request.body);
        response(ctx, result);
    }

    //订单更新酒库存
    @router('/updatewinestock', { method: 'post' })
    async updatewinestock(ctx: Koa.Context) {
        let result = await merchant_wineService.updatewinestock(ctx.request.body);
        response(ctx, result);
    }

    //品牌大使更新酒库存
    @router('/agentupdatwinesstock', { method: 'post' })
    async agentupdatwinesstock(ctx: Koa.Context) {
        let result = await merchant_wineService.agentupdatwinesstock(ctx.request.body);
        response(ctx, result);
    }

    //计算商户平台抽成比例
    @router('/calcmerchantrate', { method: 'post' })
    async calcmerchantrate(ctx: Koa.Context) {
        let result = await customer_orderService.calcmerchantrate(ctx.request.body);
        response(ctx, result);
    }


    //保存酒类配送
    @router('/savewineapply', { method: 'post' })
    async savewineapply(ctx: Koa.Context) {
        let data = await merchantapplywineService.save(ctx.request.body);
        response(ctx, data);
    }

    //查询商户申请记录
    @router('/querymapplywinebyagent', { method: 'post' })
    async querymapplywinebyagent(ctx: Koa.Context) {
        let data = await merchantapplywineService.query(ctx.request.body);
        response(ctx, data);
    }
    //审核商户酒水申请
    @router('/auditmappwinestatus', { method: 'post' })
    async audit(ctx: Koa.Context) {
        let data = await merchantapplywineService.audit(ctx.request.body);
        response(ctx, data);
    }


    //查询申请数据
    @router('/queryonewineapply', { method: 'post' })
    async queryonewineapply(ctx: Koa.Context) {
        let doc = ctx.request.body;
        let data = await merchantapplywineService.queryByid(doc.id);
        response(ctx, data);
    }

    //查询酒详情
    @router('/queryById/:id')
    async queryById(ctx: Koa.Context) {
        let data = await merchant_wineService.queryById(ctx.params.id);
        response(ctx, data);
    }

    @router('/open/:id')
    async open(ctx: Koa.Context) {
        const data = await merchant_wineService.open(ctx.params.id);
        response(ctx, '');
    }

    @router('/lock/:id')
    async lock(ctx: Koa.Context) {
        const data = await merchant_wineService.lock(ctx.params.id);
        response(ctx, '');
    }
}